package com.shopping.huaju.commodity.service.impl;

import com.shopping.huaju.commodity.entity.AttrEntity;
import com.shopping.huaju.commodity.entity.vo.BaseAttrs;
import com.shopping.huaju.commodity.service.AttrService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.huaju.common.utils.PageUtils;
import com.shopping.huaju.common.utils.Query;

import com.shopping.huaju.commodity.dao.ProductAttrValueDao;
import com.shopping.huaju.commodity.entity.ProductAttrValueEntity;
import com.shopping.huaju.commodity.service.ProductAttrValueService;

import javax.annotation.Resource;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Resource
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }
    //实现 根据 SpuSaveVo 传入的 基本属性列表，补全完整的基本属性信息入库
    @Override
    public void saveBaseAttrs(Long spuId,List<BaseAttrs> baseAttrs) {
        //1. 如果 baseAttrs为空(一般不会为空)，则不做处理--即到时候显示没有基本属性信息
        if(baseAttrs!=null && !baseAttrs.isEmpty()){
            List<ProductAttrValueEntity> attrValueEntities = baseAttrs.stream().map(baseAttr -> {
                ProductAttrValueEntity attrValueEntity = new ProductAttrValueEntity();
                //补全该对象中的属性
                attrValueEntity.setAttrId(baseAttr.getAttrId());
                attrValueEntity.setSpuId(spuId);
                attrValueEntity.setAttrSort(0);  //排序优先值默认为0;
                attrValueEntity.setQuickShow(baseAttr.getShowDesc());
                attrValueEntity.setAttrValue(baseAttr.getAttrValues());
                AttrEntity attrEntity = attrService.getById(baseAttr.getAttrId());
                attrValueEntity.setAttrName(attrEntity.getAttrName());
                return attrValueEntity;
            }).collect(Collectors.toList());
            //入库
            this.saveBatch(attrValueEntities);
        }
    }

    @Override
    public List<ProductAttrValueEntity> baseAttrlistforspu(Long spuId) {
        List<ProductAttrValueEntity> entities = this.baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        return entities;
    }

}